<?php
/**
 * 皮肤类 （通过配置可以切换使用的皮肤）
 * Enter description here ...
 * @author transn
 *
 */
class SkinAction extends BaseAction{
	
	/**
	 * 首页
	 * Enter description here ...
	 */
	function index(){
		//品牌
    	$pp=array();
    	//取所有产品的品牌
    	$pp=getProductFiled(array(),'P_BRAND');
    	$this->assign('BRAND',$pp);
    	
    	//得到地区
       $area=getAreaFiled(array(),'AR_L1');
       $this->assign('AR_L1',$area);
       
         //得到渠道
       $channel=ExUse::getChannel();
       $this->assign('channel',$channel);
        //得到订单号 该报装单唯一
      // $u_no=getOrderNumber();
      // $this->assign('orderNO',$u_no);
       
       
	   $sid=$this->in('sid');
       
       $islogin=$this->in("islogin");
       if(!$islogin){
       		$islogin='n';
	       if($_SESSION[C("USER_AUTH_KEY")]){
	       	$islogin='y';
	       }
       }
       if($sid){
       		 $seData=$_SESSION[$sid];
       }else{
       		if(ExSession::getSession()->getEMail()){
       			$seData=$_SESSION[ExSession::getSession()->getEMail()];
       			$islogin='y';
       		}
       }
       
        $seDatas=array();
       if($sid){
       		$seDatas=$seData;
       		$u_no=$sid;
       }else{
       		//得到订单号 该报装单唯一
       		$u_no=getOrderNumber();
       		if(ExSession::getSession()->getEMail()){
       			$seDatas=$seData;
       		}
       }
       $this->assign('orderNO',$u_no);
       $this->assign('islogin',$islogin);
       $this->assign('userRealName',ExSession::getSession()->getRealName());
       $this->assign('seData',$seDatas);
       
       $this->assign("menu",'1');
	   $this->display(C("USE_SKIN_PATH").":index");
	}
	/**
	 * 订单确认页面
	 * Enter description here ...
	 */
	function confirm(){
		$sId=$this->in('sid');
	  	$this->assign("sid",$sId);
	 
	  	$data=$_SESSION[$sId];
	  	
	  	$this->assign("data",$data);
	  	$this->assign("cTime",date("Y/m/d"));
		$this->display(C("USE_SKIN_PATH").":confirm");
	}
	/**
	 * 下单页面
	 * Enter description here ...
	 */
	function order(){
		//品牌
    	$pp=array();
    	//取所有产品的品牌
    	$pp=getProductFiled(array(),'P_BRAND');
    	$this->assign('BRAND',$pp);
    	
    	//得到地区
       $area=getAreaFiled(array(),'AR_L1');
       $this->assign('AR_L1',$area);
       
         //得到渠道
       $channel=ExUse::getChannel();
       $this->assign('channel',$channel);
        //得到订单号 该报装单唯一
      // $u_no=getOrderNumber();
      // $this->assign('orderNO',$u_no);
       
       
	   $sid=$this->in('sid');
       
       $islogin=$this->in("islogin");
       if(!$islogin){
       		$islogin='n';
	       if($_SESSION[C("USER_AUTH_KEY")]){
	       	$islogin='y';
	       }
       }
       if($sid){
       		 $seData=$_SESSION[$sid];
       }else{
       		if(ExSession::getSession()->getRoleShortname()=="CR" && ExSession::getSession()->getEMail()){
       			$seData=$_SESSION[ExSession::getSession()->getEMail()];
       			$islogin='y';
       		}
       }
       
        $seDatas=array();
       if($sid){
       		$seDatas=$seData;
       		$u_no=$sid;
       		 //取乡镇
			if(is_array($seDatas) && count($seDatas)){
				$seDatas["VT_NAME"]=$seDatas["AR_L4"];
			}
       }else{
       		//得到订单号 该报装单唯一
       		$u_no=getOrderNumber();
       		//只有客户才能看到自己的登录信息
       		if(ExSession::getSession()->getRoleShortname()=="CR" && ExSession::getSession()->getEMail()){
       			$seDatas=$seData;
       		}
       }
       $this->assign('orderNO',$u_no);
       $this->assign('islogin',$islogin);
       $this->assign('userRealName',ExSession::getSession()->getRealName());
      
       
       $this->assign('seData',$seDatas);
       if(ExSession::getSession()->getRealName() && ExSession::getSession()->getRoleShortname()=="CR"){
       		$this->assign("userName",ExSession::getSession()->getRealName());
       }else{
       		$this->assign("userName",'#*#');
       }
       $this->assign("menu",'2');
		$this->display(C("USE_SKIN_PATH").":order");
	}
	/**
	 * 信息输出页面
	 * Enter description here ...
	 */
	function message(){
		$this->assign("menu",'1');
		$type=$this->in("type");//类型   (是否成功)  1:成功   2:失败
   		$info=$this->in("info"); //传递过来的提示西悉尼
   		$url=$this->in("url");   //跳转的URL
   		$this->assign('type',$type);
   		$this->assign('info',$info);
   		$this->assign('url',$url);
   		$this->display(C("USE_SKIN_PATH").":message");
	}
	
	function login(){
		$sid=$this->in('sid');
		//$sid 说明来源是工单  登录成功后再回到工单
		if($sid){
			$url="?g=Home&m=Skin&a=order&islogin=y&sid=".$sid;
		}else{
			//$url="?g=Home&m=Order&a=olist";
		}
		$this->assign('sid',$sid);
		$this->assign('returnURL',$url);
		
	   	//判断是否自动登录
	   	if($_COOKIE["locationAutonLoginJKJ"]){
	   		
	   		$__userinfo=explode('|', $_COOKIE["locationAutonLoginJKJ"]);
	   		$rev=ExUse::setUser($__userinfo[0],$__userinfo[1]);
	   		if($rev){
	   			$this->redirect("?g=Home&m=Index&a=viewbg");
	   		}
	   	}
	   
	   	if($_COOKIE["locationUserJKJ"]){
	   		$__s=$_COOKIE["locationUserJKJ"];
	   		$this->assign("LOCATIONUSER",$__s);
	   	}
		
		if($this->in("ly")){
			$this->assign("menu",$this->in("ly"));
		}else{
			$this->assign("menu",'7');
		}
		
		$this->display(C("USE_SKIN_PATH").":login");
	}
	/**
	 * 商品推荐页面
	 * Enter description here ...
	 */
	function shops(){
		$this->assign("menu",'4');
		$this->display(C("USE_SKIN_PATH").":shops");
	}
	/**
	 * 订单查询页面
	 * Enter description here ...
	 */
	function olist(){
		$this->assign("menu",'3');
		if(ExSession::getSession()->getRoleShortname()=="CR" && ExSession::getSession()->getEMail()){
			
		}else{
			//没有登录 进入登录页面
			redirect("?g=Home&m=Skin&a=login");
			exit;
		}
		//每页显示多少个
		$pageSize=5;
		
		
		$order=D("Service_order");
		$search=array();
		$outSearch=array();
		//检索条件
		if($this->in("O_NO")){
			//$this->assign('O_NO',$this->in("O_NO"));
			$outSearch["O_NO"]=$this->in("O_NO");
			$search["O_NO"]=array("like","%".$this->in("O_NO")."%");
		}
		if($this->in("O_TYPE")){
			$outSearch["O_TYPE"]=$this->in("O_TYPE");
			$search["O_TYPE"]=$this->in("O_TYPE");
		}
		if($this->in("O_STATE")){
			$_ostate=$this->in("O_STATE");
			$outSearch["O_STATE"]=$_ostate;
			if($_ostate==10){
				$search["O_STATE"]=10;
			}else if($_ostate==20){
				$search["O_STATE"]=array('in',array(20,30,40,60,70,80));
			}
			else if($_ostate==30){
				$search["O_STATE"]=50;
			}
			//$search["O_STATE"]=$this->in("O_STATE");
		}
		if($this->in("CREATED_AT")){
			$outSearch["CREATED_AT"]=$this->in("CREATED_AT");
			$_time=ExUse::getTimeByCT($this->in("CREATED_AT"));
			$search["CREATED_AT"]=array('between',array(strtotime($_time['s']),strtotime($_time['e'])));
		}else{//默认显示最近一周
			$_time=ExUse::getTimeByCT(10);
			
			$search["CREATED_AT"]=array('between',array(strtotime($_time['s']),strtotime($_time['e'])));
		}
		if($this->in("CU_NAME")){
			$outSearch["CU_NAME"]=$this->in("CU_NAME");
			//$search["CU_NAME"]=array("like","%".$this->in("CU_NAME")."%");
		}
		//用户客户ID精确查询
		if($this->in("CU_ID")){
			$outSearch["CU_ID"]=$this->in("CU_ID");
			$search["CU_ID"]=$this->in("CU_ID");
		}
		if($this->in("O_MOBILE")){
			$outSearch["O_MOBILE"]=$this->in("O_MOBILE");
			$search["O_MOBILE"]=array("like","%".$this->in("O_MOBILE")."%");
		}
		if($this->in("AR_ADDRESS")){
			$outSearch["AR_ADDRESS"]=$this->in("AR_ADDRESS");
			//$search["O_MOBILE"]=array("like","%".$this->in("O_MOBILE")."%");
		}
		if($this->in("AR_ID")){
			$outSearch["AR_ID"]=$this->in("AR_ID");
			$search["AR_ID"]=$this->in("AR_ID");
		}
		if($this->in("ST_NAME")){
			$outSearch["ST_NAME"]=$this->in("ST_NAME");
			$search["ST_NAME"]=array("like","%".$this->in("ST_NAME")."%");
		}
		$this->assign('outSearch',$outSearch);
		$search['CU_ID']=ExSession::getSession()->getUserId();
	
		if($_GET['p']){
			if(count($search)){
				$list=$order->where($search)->page($_GET['p'].','.$pageSize)->select();
			}else{
				$list=$order->page($_GET['p'].','.$pageSize)->select();
			}
			
		}else{
			if(count($search)){
				$list=$order->where($search)->limit('0,'.$pageSize)->select();
			}else{
				$list=$order->limit('0,'.$pageSize)->select();
			}
		}
		
		//page使用的总数
		if(count($search)){
			//$pageList=$order->where($search)->select();
			$order1=D("Service_order");
			$pageList=$order1->where($search)->count('O_ID');
		}
		else{
			//$pageList=$order->select();
			$order1=D("Service_order");
			$pageList=$order1->count('O_ID');
		}
		
		
		$out=array();
		if($list && count($list)){
			foreach ($list as $v){
				$v["CREATED_AT"]=date("Y-m-d H:i",$v["CREATED_AT"]);
				$v["O_STATE"]=ExUse::getOrderState($v["O_STATE"]);
				$v["O_TYPE"]=ExUse::getOrderType($v["O_TYPE"]);
				//去地区
				$AR_ID=$v["AR_ID"];
				$tmpData=ExUse::getArea($AR_ID);
				
				$STR=implode(' ',$tmpData);
				$vname=ExUse::getVTName($v["VT_ID"]);
				if(!$vname){
					$vname='';
				}
				$v["O_AREA"]=$STR.' '.$vname;//.' '.$v["O_ADDRESS"];
				$v["O_PRODUCT"]=$v["P_BRAND"].' '.$v["P_CATEGORY"].' '.$v["P_MODEL"];
				
				$out[]=$v;
			}
		}
		$this->assign('orderList',$out);
		//得到类型
		$types=C("ORDER_TYPE");
		$type=array();
		$type[]=array('id'=>'','name'=>'全部');
		foreach ($types as $tk=>$tv){
			$type[]=array('id'=>$tk,'name'=>$tv);
		}
		//$type=array(array('id'=>'','name'=>'全部'),array('id'=>'1','name'=>'报装'),array('id'=>'2','name'=>'保修'));
		$state=ExUse::getCROrderStateArr();
		$states=array();
		$states[]=array("id"=>'','name'=>'全部');
		foreach ($state as $key=>$val){
			$states[]=array("id"=>$key,'name'=>$val);
		}
		
		$this->assign('type',$type);
		$this->assign('state',$states);
		
		//取创建时间
		$createTime=ExUse::getCreatedTime();
		$this->assign('createTime',$createTime);
		
		import('ORG.Util.Page');
		//$count      = count($pageList);// 查询满足要求的总记录数
		$count      = $pageList;// 查询满足要求的总记录数
		$Page       = new Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('theme', "总订单数 %totalRow%    &nbsp;|&nbsp;%nowPage%/%totalPage% 页     &nbsp;&nbsp;    %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%");
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		
		
	   	$msg=ExSession::getSession()->getRealName()."在".formatDate(time())."查看服务单。";
   		ExLog::addLog($msg, 'TYPE_ORDER_VIEW');
		
		
		$this->assign("cTime",date("Y/m/d"));
		$this->display(C("USE_SKIN_PATH").":list");
	}
	/**
	 * 显示订单详情
	 * Enter description here ...
	 */
	function detail(){
		$O_ID=$this->in("O_ID");
		$this->assign("o_id",$O_ID);
		$order=D("Service_order");
		$data=$order->where('O_ID='.$O_ID)->select();
		if($data){
			$tmpData=$data[0];
			$tmpData["O_STATE"]=$tmpData["O_STATE"];
			$tmpData["O_STATE_NAME"]=ExUse::getOrderState($tmpData["O_STATE"]);
			$tmpData["O_TYPE"]=ExUse::getOrderType($tmpData["O_TYPE"]);
			
			$tmpData["O_F_STATE"]=ExUse::getJsState($tmpData["O_F_STATE"]);
			
			$tmpData["O_REQ_TIME"]=formatDate($tmpData["O_REQ_TIME"]);
			$tmpData["O_SRV_B_TIME"]=formatDate($tmpData["O_SRV_B_TIME"]);
			$tmpData["O_SRV_TIME"]=formatDate($tmpData["O_SRV_TIME"]);
			
			//去地区
			$AR_ID=$tmpData["AR_ID"];
			$tData=ExUse::getArea($AR_ID);
			
			$STR=implode(' ',$tData);
			$vname=ExUse::getVTName($tmpData["VT_ID"]);
			if(!$vname){
				$vname='';
			}
			$tmpData["O_AREA"]=$STR." ".$vname;//.' '.$tmpData["O_ADDRESS"];
			$tmpData["O_PRODUCT"]=$tmpData["P_BRAND"].'，'.$tmpData["P_CATEGORY"].'，'.$tmpData["P_MODEL"];
			$tmpData["P_BUY_TIME"]=formatDate($tmpData["P_BUY_TIME"],'Y-m-d');
			$uname=ExUse::getUname($tmpData["CU_ID"],'U_NAME');
			$tmpData["CU_ID_Name"]='';
			if($uname){
				$tmpData["CU_ID_Name"]=$uname;
			}
			if($this->in("zp")){
				//看看是否存在服务队
				$u_id=ExSession::getSession()->getUserId();
				$u_name=ExSession::getSession()->getRealName();
				$sst=D("Srv_team_members");
				$sst->find($u_id);
				$st_id=$sst->ST_ID;
				if($st_id){
					$st=D("Srv_team");
					$st->find($st_id);
					$ST_NAME=$st->ST_NAME;
					$order=D("Service_order");
					$order->find($O_ID);
					$order->ST_ID=$st_id;
					$order->ST_NAME=$ST_NAME;
					$order->SRV_ID=$u_id;
					$order->SRV_NAME=$u_name;
					$order->O_SRV_B_TIME=time();//服务开始时间
					$order->O_STATE=C("ORDER_STATE.STATE_EXECUTING");//设置状态为已派发
					$logOid=$O_ID;
					$logONo=$order->O_NO;
					$rev=$order->save();
					if($rev){
						$msg=ExSession::getSession()->getRealName()."在".formatDate(time())."处理服务单。";
	   					ExLog::addLog($msg,'TYPE_ORDER_EXECUTE',2,$logOid,$logONo);
					}
				}
			}
			
		}
		//取服务队的电话
		$t_id=$tmpData["ST_ID"];
		$__team=D("Srv_team");
		$__team->find($t_id);
		$ST_MOBILE=$__team->ST_MOBILE;
		$ST_PHONE=$__team->ST_PHONE;
		$tmpData["ST_MOBILE"]=$ST_MOBILE;
		$tmpData["ST_PHONE"]=$ST_PHONE;
		
		$this->assign("data",$tmpData);
		$this->assign("cTime",date("Y/m/d"));
		$this->assign("menu",'3');
		$this->display(C("USE_SKIN_PATH").":detail");
	}
	
	function pwd(){
		if($this->in("op")){
			$U_NAME=$this->in("U_NAME");
			$bz='';
			$user=D("User");
			//会员名/邮箱  
			$IS_USER_OK=false;
			$user=D("User");
			$search=array();
			$search["U_EMAIL"]=$U_NAME;
			$data=$user->where($search)->select();
			if($data){
				$IS_USER_OK=true;
				$bz="email";
			}else{
				//检查是否手机登录
				$user=D("User");
				$search=array();
				$search["U_MOBILE"]=$U_NAME;
				$data=$user->where($search)->select();
				if($data){
					$IS_USER_OK=true;
					$bz="phone";
				}
			}
			$__out='n';
			if($IS_USER_OK){
				//生成随机密码
				$__pwd=mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9); 
				$uid=$data[0]["U_ID"];
				$user=D("User");
				$user->find($uid);
				$user->U_PWD=md5($__pwd);
				$rev=$user->save();
				if($rev){
					$__content=C("GETPWD_EMAIL");
					$t=sprintf($__content,$__pwd);
					if($bz=="email"){
						Mail::getMail()->sendMail($U_NAME,'密码修改',$t);
					}
					if($bz=="phone"){
						Mobile::getMobile()->send($U_NAME,$t);
					}
					$__out='y';
				}
			}
			echo $__out;
			exit;
		}
		$ly='7';
		if($this->in("ly")){
			$ly=$this->in("ly");
		}
		$this->assign("menu",$ly);
		$this->display(C("USE_SKIN_PATH").":pwd");
	}
	/**
	 * 修改密码
	 * Enter description here ...
	 */
	function changePwd(){
		$uid=$this->in("uid");
		if(!$uid){
			$uid=ExSession::getSession()->getUserId();
		}
		if(!$uid){
			echo "noLogin";
			exit;
		}
		if($this->in("op")){
			$data=$this->in("data");
			$user=D("User");
			$user->find($uid);
			$_pwd=$user->U_PWD;
			if(md5($data["U_PWD_OLD"])!=$_pwd){
				echo "p";
				exit;
			}
			$user->U_PWD=md5($data["U_PWD"]);
			$rev=$user->save();
			if($rev){
				echo ExSession::getSession()->getRoleShortname();
			}else{
				echo "n";
			}
			exit;
			
		}
		$this->assign("uid",$uid);
		$this->display(C("USE_SKIN_PATH").":changePwd");
	}
	
}